import { message } from 'ant-design-vue';

// 二进制格式
export function dataURItoBlob(dataURI) {
  // base64 解码
  let byteString = window.atob(dataURI.split(',')[1]);
  let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  let ab = new ArrayBuffer(byteString.length);
  let ia = new Uint8Array(ab);
  for (let i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
  }
  return new Blob([ab], {
    type: mimeString,
  });
}

/**
 * 上传前检查
 * @param file
 * @returns {boolean}
 */
export function beforeAvatarUpload(file) {
  const isLt4M = file.size / 1024 / 1024 < 4;
  if (!isLt4M) {
    message.error('上传图片大小不能超过4MB!');
    return false;
  }
  return isLt4M;
}
/**
 * 文件检查
 * @param file
 * @param sides
 * @param callback
 * @returns {boolean}
 */
export function fileCheck(file, callback) {
  console.log(file,'file');
  const isLt4M = file.size / 1024 / 1024 < 4;
  if (!isLt4M) {
    message.error('上传图片大小不能超过 4MB!');

    callback({
      flag: false,
      imageUrl: '',
    });
    return;
  }
  var reader = new FileReader();
  reader.readAsDataURL(file);
  let imageUrl = ''
  reader.onload = function () {
    //让页面中的img标签的src指向读取的路径
    var img = new Image();
    img.src = reader.result;
    console.log(img.width);

    //如果存在浏览器缓存中
    if (img.width > 4096 || img.height > 4096) {
      message.error('上传图片最长边不大于 4096!');
      callback({
        flag: false,
        imageUrl: '',
      });
    } else {
      imageUrl = URL.createObjectURL(file);
      // 解析成Base64
      //   vm.idCard1 = reader.result;
      if (callback && callback instanceof Function) {
        callback({
          flag: true,
          imageUrl: reader.result,
        });
      }
    }
  };
}
/*
 * 反面上传成功
 * @param res
 * @param file
 */
export function handleAvatarSuccess2(res, file) {
  //   const loading = this.$loading({
  //     lock: true,
  //     text: 'Loading......',
  //     spinner: 'el-icon-loading',
  //     background: 'rgba(0, 0, 0, 0.7)',
  //   });
  // this.imageUrl = URL.createObjectURL(file.raw);
  this.ocrLoading = true;
  if (this.idCard2 == null || this.idCard2 == '') {
    this.$message.error('请上传国徽面照片！');
    this.ocrLoading = false;
    return;
  }
  let Formdata = new FormData();
  let idCardZ = this.dataURItoBlob(this.idCard2);
  Formdata.append('file', idCardZ);
  Formdata.append('type', 'F');
  // operateType = 1 新增、2修改、3修改审核结果、4重新上传
  if (this.form.id != null && this.operateType == '4') {
    this.$message.warning('国徽面照片上传中');
    Formdata.append('id', this.form.id);
    axios
      .post('/credit/singleUpload', Formdata)
      .then((res) => {
        if (res.data.code == '2000000') {
          this.ocrLoading = false;
          this.form.backImageUrl = res.data.data.imageUrl;
          this.form.backOcrId = res.data.data.imageId;
          if (res.data.data.certEndDate && res.data.data.imageUrl && res.data.data.imageId) {
            this.form.backImageStatus = '1';
          } else {
            this.form.backImageStatus = '0';
          }
          loading.close();
          this.$message.success('国徽面照片上传成功');
          this.$emit('ocedata', this.datas, false, this.dataIndex); // 子传父
          return;
        } else {
          this.ocrLoading = false;
          this.form.backImageUrl = res.data.data.imageUrl;
          this.form.backOcrId = res.data.data.imageId;
          this.form.backImageStatus = '0';
          loading.close();
          this.$message.error('国徽面照片上传失败');
          //this.$emit('ocedata', '', true, ''); // 子传父
        }
      })
      .catch((err) => {
        this.ocrLoading = false;
        loading.close();
        this.$message.error('国徽面照片上传失败');
        //this.$emit('ocedata', '', true, ''); // 子传父
      });
  } else {
    this.$message.warning('国徽面照片OCR识别中');
    axios
      .post('ocr/singleQuery', Formdata)
      .then((res) => {
        if (res.data.data.certStartDate && (res.data.data.certEndDate || (!res.data.data.certEndDate && res.data.data.certLongTerm == 'Y'))) {
          this.ocrLoading = false;
          this.form.backImageUrl = res.data.data.imageUrl;
          this.form.backOcrId = res.data.data.imageId;
          if (res.data.data.certStartDate && res.data.data.imageUrl && res.data.data.imageId) {
            this.form.backImageStatus = '1';
            loading.close();
            this.$message.success('国徽面照片识别成功');
          } else {
            this.form.backImageStatus = '0';
            loading.close();
            this.$message.error('国徽面照片识别失败');
          }
          this.$emit('ocedata', this.datas, false, this.dataIndex); // 子传父
          return;
        } else {
          this.ocrLoading = false;
          this.form.backImageUrl = res.data.data.imageUrl;
          this.form.backOcrId = res.data.data.imageId;
          this.form.backImageStatus = '0';
          loading.close();
          this.$message.error('国徽面照片识别失败');
          //this.$emit('ocedata', '', true, ''); // 子传父
        }
      })
      .catch((err) => {
        this.ocrLoading = false;
        loading.close();
        this.$message.error('国徽面照片识别失败');
        //this.$emit('ocedata', '', true, ''); // 子传父
      });
  }
}
